package com.dts.evaluate.ui.activity;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.dts.evaluate.R;
import com.dts.evaluate.common.SocketType;
import com.dts.evaluate.utils.DensityUtil;
import com.lzy.okhttputils.OkHttpUtils;

/**
 * 基类activity
 */
public class BaseActivity extends AppCompatActivity {

    private View mDecorView;
    private LayoutInflater mInflater;
    private View progressView;
    private AlertDialog dialog;

    private AttendanceReceiver receiver;

    private boolean isShow = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // 隐藏应用程序的标题栏，即当前activity的标题栏
        //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDecorView = getWindow().getDecorView();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        mInflater = LayoutInflater.from(this);
        progressView = mInflater.inflate(R.layout.dialog_progress_layout, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.dialog);

        builder.setView(progressView);
        dialog = builder.create();

        //注册考勤服务
        receiver = new AttendanceReceiver();
        IntentFilter intentFilter = new IntentFilter(SocketType.CALL_BACK_ATTENDANCE);
        registerReceiver(receiver,intentFilter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        hideSystemUI();
        isShow = true;
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        hideSystemUI();
    }

    @Override
    protected void onPause() {
        super.onPause();
        isShow = false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
        unregisterReceiver(receiver);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_HOME) {
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    private void hideSystemUI() {
        // Set the IMMERSIVE flag.
        // Set the content to appear under the system bars so that the content
        // doesn't resize when the system bars hide and show.
        mDecorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                        | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);
    }


    /**
     * 显示进度
     */
    protected void showProgress() {
        dialog.show();

    }

    /**
     * 隐藏进度
     */
    protected void hideProgress() {
        dialog.dismiss();
    }

    /**
     * 显示失败对话框
     *
     * @param title
     * @param message
     */
    protected void showErrorDialog(String title, String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        TextView tv = new TextView(this);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        tv.setLayoutParams(layoutParams);
        tv.setTextSize(DensityUtil.sp2px(this, 20));
        tv.setPadding(20, 20, 20, 20);
        tv.setGravity(Gravity.CENTER);
        tv.setText(Html.fromHtml(message));
        tv.setTextColor(ContextCompat.getColor(this,R.color.dialog_error_text_color));
        builder.setTitle(title);
        builder.setView(tv);
        builder.setNegativeButton(R.string.sure, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    /**
     * 显示成功对话框
     *
     * @param title
     * @param message
     */
    protected void showSuccessDialog(String title, String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        TextView tv = new TextView(this);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        tv.setLayoutParams(layoutParams);
        tv.setGravity(Gravity.CENTER);
        tv.setTextSize(DensityUtil.sp2px(this, 20));
        tv.setPadding(20, 20, 20, 20);
        tv.setText(Html.fromHtml(message));
        tv.setTextColor(ContextCompat.getColor(this,R.color.dialog_success_text_color));
        builder.setTitle(title);
        builder.setView(tv);
        builder.setNegativeButton(R.string.sure, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    /**
     * 考勤系统的广播接收
     */
    class AttendanceReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(SocketType.CALL_BACK_ATTENDANCE.equals(action)){
                if(isShow){

                }
            }
        }
    }



}
